﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace @class
{
    abstract class Shape
    {
        public abstract double calculateArea();
        public abstract bool isvalid();
    }
    class rectangle : Shape
    {
        public double Width { get; set; }
        public double Height { get; set; }
        public rectangle(double width, double height)
        {
            Width = width;
            Height = height;
        }
        public override double calculateArea()
        {
            return Width * Height;
        }
        public override bool isvalid()
        {
            return (Width > 0 && Height > 0);
        }
    }
    class Square : Shape
    {
        public double Side { get; set; }
        public Square(double side)
        {
            Side = side;
        }
        public override double calculateArea()
        {
            return Side * Side;
        }
        public override bool isvalid()
        {
            return (Side > 0);
        }
    }
    class Triangle : Shape
    {
        public double Edge1 { get; set; }
        public double Edge2 { get; set; }
        public double Edge3 { get; set; }
        public Triangle(double edge1, double edge2, double edge3)
        {
            Edge1 = edge1;
            Edge2 = edge2;
            Edge3 = edge3;
        }
        public override double calculateArea()
        {
            double s = (Edge1 + Edge2 + Edge3) / 2;
            return Math.Sqrt(s * (s - Edge1) * (s - Edge2) * (s - Edge3));
        }
        public override bool isvalid()
        {
            return (Edge1 > 0 && Edge2 > 0 && Edge3 > 0 && Edge1 + Edge2 > Edge3 && Edge1 + Edge3 > Edge2 && Edge3 + Edge2 > Edge1);
        }
    }
    class Shapefactory
    {
        private static Random random = new Random();
        public static Shape CreateShape()
        {
            // 随机生成一个形状类型
            int shapeType = random.Next(0, 3); // 0: rectangle, 1: Square, 2: Triangle

            switch (shapeType)
            {
                case 0:
                    double width = random.NextDouble() * 10 + 1; 
                    double height = random.NextDouble() * 10 + 1; 
                    return new rectangle(width, height);

                case 1:
                    double side = random.NextDouble() * 10 + 1; 
                    return new Square(side);

                case 2:
                    double edge1 = random.NextDouble() * 10 + 1;
                    double edge2 = random.NextDouble() * 10 + 1;
                    double edge3 = random.NextDouble() * 10 + 1;
                    while (!(edge1 + edge2 > edge3 && edge1 + edge3 > edge2 && edge2 + edge3 > edge1))
                    {
                        edge1 = random.NextDouble() * 10 + 1;
                        edge2 = random.NextDouble() * 10 + 1;
                        edge3 = random.NextDouble() * 10 + 1;
                    }
                    return new Triangle(edge1, edge2, edge3);

                default:
                    throw new InvalidOperationException("Invalid shape type");
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            List<Shape> shapes = new List<Shape>();
            double allcal = 0;
            // 创建10个随机形状
            for (int i = 0; i < 10; i++)
            {
                Shape shape = Shapefactory.CreateShape();
                shapes.Add(shape);
                allcal += shape.calculateArea();
                Console.WriteLine(allcal);
            }
        }
    }
}
